/*本模块功能:作业管理详情
        编写者:朱光曜
        编写时间:2017.11.15*/
$(function() {
	var string1 = '<tr>' +
		'<th class="col-lg-1">序号</th>' +
        '<th class="col-lg-2">学号</th>' +
		'<th class="col-lg-2">学生姓名</th>' +
		'<th class="col-lg-1">作业</th>' +
		'<th class="col-lg-1">作业评价</th>' +
		'<th class="col-lg-1">评价/评分</th>' +
		'<th class="col-lg-4">操作</th>' +
		'</tr>';
	/*头部内容*/
	var confirm1 = new Confirm;
	var Alert1 = new Alert;
	var Alert2 = new Alert;
    var guitarLesson_bool=true;
	/*function locationInformationd(data) { //搜索
		
	}*/
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
		loading1.show();
		
		});
	$(document).ajaxStop(function(){
		loading1.hide();
		
		});
	
	function tabelModel(data) { //列表
		var str = "";
		var endMark = "";
		var str1 = "";
		var str2="";//评价,评分
		var speak = "";
		var speakAttrs="";//评价内容
		var taskAddress="";//作业文件
		if(data.homeworkList[key].taskAddress) {
			str = "已提交";
			endMark = "see";
			
			taskAddress=data.homeworkUrl+data.homeworkList[key].convertFile;
			if(data.homeworkList[key].taskFraction) {
				str1 = "已评价";
				speakAttrs=data.homeworkList[key].submitContent;
				speak = "speak";
		
				if(data.sorceMap.souceMark||data.sorceMap.souceMark==0){
					if(data.sorceMap.souceMark==0){
						str2=data.homeworkList[key].taskFraction;
					}
	                if(data.sorceMap.souceMark==1){
	                	str2=data.homeworkList[key].taskSoruce;
					 
						
					}
					
				}else{
					str2="暂无";
				}
			} else {
				str1 = "未评价";
				str2="暂无";
				speak = "speak";
				
			}
			
			
		} else {
			str = "未提交";
			speak = "";
		
			//guitarLesson_bool=true;
			str1 = "未评价";
			str2="暂无";
		}
		
		data.classEndMark=0;
		if(data.classEndMark==0){
			
			$(".funBox .guitarLessons").html("未结课");
		}else{
			speak = "";
		
			$(".funBox .guitarLessons").html("已结课");
		}
		return '<tr data-num="0" chapterNum="' + data.homeworkList[key].chapterNum + '" courseId="' + data.homeworkList[key].courseId + '" stuorteaId="' + data.homeworkList[key].stuorteaId + '">' +
			'<td class="col-lg-2">' +((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
            '<td class="col-lg-10"  title="">' + data.homeworkList[key].stuorteaId + '</td>' +
			'<td class="col-lg-10 userName"  title="">' + data.homeworkList[key].userName + '</td>' +
			'<td class="col-lg-10"  title="">' + str + '</td>' +
			'<td class="col-lg-10"  title="">' + str1 + '</td>' +
			'<td class="col-lg-10"  title="">' + str2 + '</td>' +
			'<td class="col-lg-4">' +
			'<a href="javascript:;" taskAddress="' + taskAddress + '" class="seeExperiment  ' + endMark + '">查看作业</a>' +
			'<a style="display: none;" href="javascript:;" class="speakExperiment  ' + speak + '" speakAttrs="'+speakAttrs+'" chapterNum="' + data.homeworkList[key].chapterNum + '" courseId="' + data.homeworkList[key].courseId + '" stuorteaId="' + data.homeworkList[key].stuorteaId + '"  score="'+str2+'" >评价作业</a>' +
			'</td>' +
			'</tr>';

	}

	function speakPresentation(data,point) { //评价作业
		var str="";//状态
		//if(data.sorceMap){}
		if(data.sorceMap){
			
			if(data.sorceMap.souceMark==0){
				str='<select id="selectState1" class="publicSelect fl" >' +
				'<option value="">优</option>' +
				'<option value="1">良</option>' +
				'<option value="2">中</option>' +
				'<option value="3">差</option>' +
				'</select>';
			}
            if(data.sorceMap.souceMark==1){
            	str='<input  type="text" id="selectState1" class="publicText" placeholder="请输入评分" />';
			}
		}else{
		
			index.alert1("您当前未设置权重值,请在成绩设置处设置权重值");
			return;
		}
		
		return '<div class="evaluate">' +
			'<p class="popup_contenthead">作业评价<span class="fr closes"><img src="pc/images/ico_alertDel.png" alt="叉" /></span></p>' +
			'<div class="clear"></div>' +
			'<div class="pop_main">' +
			'<div ><label for="departNames" class="fl">作业名称:&ensp;</label><div  class="fl tittle" title="'+$(".mainContent .funBox .tittle span.chapterName").html()+'">'+$(".mainContent .funBox .tittle span.chapterName").html()+'</div></div>' +
			'<div class="clear"></div>' +
			'<div class="inputRight"><label for="departNames"  class="fl">作业评分:&ensp;</label>' +
			str+
			'</div>' +
			'<div class="clear"></div>' +
			'<div ><label for="departNames"  class="fl">作业评价:&ensp;</label>' +
			'<textarea name="" rows="" cols=""  class="fl" id="jobEvaluation" placeholder="请输入评价" >'+point.attr("speakattrs")+'</textarea>' +
			'</div>' +
			'</div>' +
			'<div class="down">' +
			'<div class="publicBtn preservation"   chapterNum="' + point.attr("chapterNum") + '" courseId="' + point.attr("courseId") + '" stuorteaId="' + point.attr("stuorteaId") + '">保存</div>' +
			'</div>' +
			'</div>';
	}

	function seePresentation(data1) { //查看报告
		return '<div class="presentation">' +
			'<p class="tittle"><span class="classes">'+data1[6] +'</span><span></span>.作业报告详情<span class="fr closes"><img src="pc/images/ico_alertDel.png" alt="叉"/></span></p>' +
			'<iframe src="'+data1[4]+'" width="100%" height="86%"></iframe>'+
			'</div>'+
			'<div class="publicBtn speakPresentation" speakattrs="'+data1[0]+'" chapterNum="' + data1[1] + '" courseId="' + data1[2] + '" stuorteaId="' + data1[3] + '"  score="'+data1[5] +'" >评价作业</div>';
	}
	var nowData = 1;
	var index = {
        some:'',
		init: function() {

			this.bind();
		},
		bind: function() {
			var self = this;
			this.data_content = {}; //搜索集
			this.tab_content = {}; //列表集
			this.parameter = {
				"pageNow": 1,
				"pageSize": 10,
				"chapterNum":result.paramData.chapterNum,
				"classId":result.paramData.classId,
				"courseId":result.paramData.courseId
			};
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    this.parameter = result.paramData.some;
                    if( this.parameter.state!=undefined&&this.parameter.state!=null){
                        $("#selectState").val(this.parameter.state);
                    }else{
                        $("#selectState").children("option").eq(0).attr("selected",true);
                    }
                    if( this.parameter.userName!=undefined){
                        $("#studentName").val(this.parameter.userName);
                    }
                }
            }
            self.some = this.parameter;
			$(".publicText").attr("maxlength","20");
			
			$.map($(".publicText"),function (opt) {//长度限制
				maxLengthLimit.init(opt);
			});
			$(".publicText").bind('input propertychange',function(){
				
			    var regy = /\s/g;
				if(regy.test($(this).val())){
					$(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
				}
			});
			this.postData(this.parameter, nowData);
            $(".looktg").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName": "teacherJobManagement",
                    "paramData": {
                        "chapterNum": result.paramData.chapterNum,
                        "courseId": result.paramData.courseId,
                        "classId": result.paramData.classId,
                        "chapterId": result.paramData.chapterId,
                        "some": result.paramData.somes
                    }
                });

                window.location.href = "skipPage?jsonStr=" + json;
            });
		},
		postData: function(par_data, nowData, hunt) { //获得搜索及列表数据
			var self = this;
			var parameter = par_data;

			$.ajax({
				type: "post",
				url: "skipHomeworkDetail",
				async: true,
				data: {
					"jsonStr": JSON.stringify(parameter)
				},
				success: function(data, status, xhr) {
					var new_data = JSON.parse(data);

					if(hunt) {
						self.tab_content = new_data;

						self.tab_add();
					} else {

						self.data_content = new_data;
						self.tab_content = new_data;

						self.add();
						self.tab_add();
					}
					// console.log(new_data.totalPage+"啊啊"+new_data.pageNow)
					var sumpage = new_data.totalPage;
					if(sumpage<1){sumpage=1;};
					var nowpage = new_data.pageNow;
					var pagenumber = 10;
					var parame = parameter;
					$(".tcdPageCode").createPage({
						pageCount: sumpage, //总页数
						current: nowpage, //当前页数
						pageNumber: pagenumber, //每页显示数量
						backFn: function(p) {

							nowpage = p;

							if(nowpage >= sumpage) {
								nowpage = sumpage;
							}
							if(!parameter) {
								parameter = {
									"pageNow": nowpage,
									"pageSize": 10
								};
							
							} else {
								parameter.pageNow = nowpage;
							
							}
							$.ajax({
								type: "post",
								url: "skipHomeworkDetail",
								async: true,
								data: {
									"jsonStr": JSON.stringify(parameter)
								},
								async: true,
								success: function(msg) {
									//获取课程
									var msgNew = JSON.parse(msg);
									self.tab_content = msgNew;

									self.tab_add();
								},
								error: function(data, status, xhr) {
									var json = JSON.stringify({
										"pageName": "error"
								    });
									window.location.href = "skipPage?jsonStr=" + json;

								}
							});
						}
					});
				},
				error: function(data, status, xhr) {
					var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

				}
			});
		},
		add: function() { //搜索追加
			var self = this;
			var data = self.data_content;

			/*$("#searchCriteria").html(locationInformationd(data));*/
			$(".mainContent .funBox .tittle span.classes").html(data.classMsg.className);

			$(".mainContent .funBox .tittle span.chapterName").html(data.chapterMsg.chapterName);
			$(".funBox .searchBtn").off("click").click(function() { //搜索点击

				var userName = $("#studentName").val();
				if(verification.specialCode(userName) !== true) {
					self.alert1("不能输入特殊字符(@/'\"#$%&^*)");
					return;
				}
				var state = $("#selectState option:selected").attr("value");
				var nowData = 1;
				var parameter = {
					
					"userName": userName,
					"chapterNum":result.paramData.chapterNum,
					"classId":result.paramData.classId,
					"courseId":result.paramData.courseId,
					
					
					"state": state,
					"pageNow": 1,
					"pageSize": 10
				};
				var j=0;
				//遍历删除空元素
				for(i in parameter){
					if(j==0){j++;continue;}
					if(parameter[i]==null||parameter[i]==""){
						delete parameter[i];
					}
					if(!isNaN(parameter[i])){
						parameter[i]=Number(parameter[i]);
					}
				}

                self.some = parameter;
				self.postData(parameter, nowData, 1);
			});

		},
		tab_add: function() { //列表追加

			var self = this;
			var data = self.tab_content;
			var table = string1;

			//data.selectList={};

			var i = 0;
			for(key in data.homeworkList) {
				i++;
			}

			if(i > 0) {

				for(key in data.homeworkList) {

					table += tabelModel(data);
				}

				$(".mainContent .tableBox").html(table);
			} else {
				var trImg = "<tr><td colspan='7'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
				$(".mainContent .tableBox").html(string1 + trImg);
			}
            
			//设置列表宽度(最大为9)
			var numMax = 0;
			$(".tableBox tr th").each(function() {
				var str = $(this).attr("class");
				var num = str.lastIndexOf("col-lg-") + 7;
				var num1 = parseFloat(str.substring(num, num + 1));
				numMax += num1;
			});
			
			$(".tableBox tr th").each(function() {
				var str = $(this).attr("class");
				var num = str.lastIndexOf("col-lg-") + 7;
				var num1 = parseFloat(str.substring(num, num + 1));
			
				$(this).css("width", num1 / numMax * 100 + "%");
			});
            
            
			$(".see").off("click").on("click", function() { //查看实验报告
				var stuorteaId=$(this).parents("tr").attr("stuorteaId");
				var chapterNum=$(this).parents("tr").attr("chapterNum");
				var courseId=$(this).parents("tr").attr("courseId");
                // 获取数据
                var json = JSON.stringify({"stuorteaId":stuorteaId,"chapterNum":chapterNum,"courseId":courseId});
                $.ajax({
                    type: "post",
                    url: "getMyHomeworkDetail",
                    async: true,
                    data: {
                        "jsonStr": json
                    },
                    success: function(msg) {
                        var msg = JSON.parse(msg);
                        if(msg.result == "success"){
                            var json = JSON.stringify({
                                "pageName":"stuHomeDetail",
                                "paramData":{
                                    "stuorteaId":stuorteaId,
                                    "chapterNum":chapterNum,
                                    "courseId":courseId,
                                    "classId":result.paramData.classId,
                                    "chapterName":$(".chapterName").html(),
                                    "chapterId":result.paramData.chapterId,
                                    "some":self.some,
                                    "somes": result.paramData.somes
                                }
                            });
                            window.location.href = "skipPage?jsonStr="+json;
                        }else{
                            Alert1.content = msg.failMsg;
                            Alert1.init();
                            Alert1.show();
                        }


                    },
                    error: function(data, status, xhr) {
                        var json = JSON.stringify({
                            "pageName": "error"
                        });
                        window.location.href = "skipPage?jsonStr=" + json;

                    }
                });
				
			});
		
			$(document).off("click", ".speak").on("click", ".speak", function(event) { //评价作业
				var data1=data;
			
				var point=$(this);
				$("#Popup").css({"display": "block"}).html(speakPresentation(data1,point));
				$("#popupBackground").css("display","block");
				if($("input#selectState1").length==1){
                $("#selectState1").attr("maxlength","6");
				
				$.map($("#selectState1"),function (opt) {//长度限制
					maxLengthLimit.init(opt);
				});
				};
               $("textarea").attr("maxlength","200");
				
				$.map($("textarea"),function (opt) {//长度限制
					maxLengthLimit.init(opt);
				});
				$("textarea").bind('input propertychange',function(){
					
				    var regy = /\s/g;
					if(regy.test($(this).val())){
						$(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
					}
				    });
				
				if(point.attr("score")||point.attr("score")==0){
					
					if(isNaN(point.attr("score"))){
						$("#selectState1 option").each(function(){
							if($(this).html()==point.attr("score")){
								$(this).attr("selected","selected");
							}
						});
					}else{
						$("#selectState1").val(point.attr("score"));
					}
				}

			});
			$(document).off("click", ".speakPresentation").on("click", ".speakPresentation", function(event) { //详情评价作业
				var data1=data;
				
				var point=$(this);
				$("#Popup").css({
					"display": "block"
				}).html(speakPresentation(data1,point));
				$("#popupBackground").css("display","block");
				if($("input#selectState1").length==1){
					$("#selectState1").attr("maxlength","6");
					
					$.map($("#selectState1"),function (opt) {//长度限制
						maxLengthLimit.init(opt);
					});
				}
               $("textarea").attr("maxlength","200");
				
				$.map($("textarea"),function (opt) {//长度限制
					maxLengthLimit.init(opt);
				});
				$("textarea").bind('input propertychange',function(){
					
				    var regy = /\s/g;
					if(regy.test($(this).val())){
						$(this).val($(this).val().replace(/\s|\xA0/g,"")) ;
					}
				    });
				
				
				if(point.attr("score")||point.attr("score")==0){
					
					if(isNaN(point.attr("score"))){
						$("#selectState1 option").each(function(){
							if($(this).html()==point.attr("score")){
								$(this).attr("selected","selected");
							}
						});
					}else{
						$("#selectState1").val(point.attr("score"));
					}
				}
			});
			
			$(document).off("click",  "#Popup .closes").on("click", "#Popup .closes", function() {//关闭弹窗
				$("#Popup").css("display", "none").html("");
				$("#popupBackground").css("display","none");
			});
			$(document).off("click",  "#Popup1 .closes").on("click", "#Popup1 .closes", function() {//关闭弹窗
				$("#Popup1").css("display", "none").html("");
		
			});
			$(document).off('click','.preservation').on("click", ".preservation", function() {//提交按钮
				//$(this).unbind("click");
				var chapterNum=parseInt($(this).attr("chapterNum"));
				var courseId=parseInt($(this).attr("courseId"));
				var stuorteaId=$(this).attr("stuorteaId");
				var submitContent=$("#jobEvaluation").val();
				if($('select#selectState1').length==1){
						var taskFraction=$('#selectState1 option:selected' ).html();
						if(verification.specialCode(submitContent)!==true){
		    				self.alert1("不能输入特殊字符(@/'\"#$%&^*)","2");
		    				return;
		    			}
						var assessTestParameters={
							"chapterNum":chapterNum,
							"courseId":courseId,
							"stuorteaId":stuorteaId,
							"submitContent":submitContent,
							"taskFraction":taskFraction
						};	
				}
			
			    if($('input#selectState1').length==1){
			    		var taskSoruce=$('#selectState1').val();
			    		var n = /^[1-9]\d*$|^0$/;
			    		if(taskSoruce==""){
		                	self.alert1("请输入评价分数","2");
		                	return;
		                }else if(!n.test(taskSoruce)) {
		    				self.alert1("请输入数字信息","2");
		    				return;
		    			}else{
		                	taskSoruce=Number(taskSoruce);
		                }
			    		if(verification.specialCode(submitContent)!==true){
		    				self.alert1("不能输入特殊字符(@/'\"#$%&^*)","2");
		    				return;
		    			}

						var assessTestParameters={
							"chapterNum":chapterNum,
							"courseId":courseId,
							"stuorteaId":stuorteaId,
							"submitContent":submitContent,
							"taskSoruce":parseInt(taskSoruce)	
						};
				}
			
				$.ajax({
					type: "post",
					url: "assessHomework",
					async: true,
					data: {
						"jsonStr": JSON.stringify(assessTestParameters)
					},
				
					success: function(msg) {
						var courseIds = courseId;
						var msgNew = JSON.parse(msg);
				        $("#Popup1").css("display", "none").html("");
				        $("#Popup").css("display", "none").html("");
				        $("#popupBackground").css("display","none");
						if(msgNew.result == "success") {
							self.alert1("评价成功");
						} else {
							if(msgNew.failMsg!=undefined){
                                self.alert1(msgNew.failMsg);
							}else{
                               // self.alert1("评价失败");
                                if(msgNew.failMsg){
    								self.alert1(msgNew.failMsg);
    							}else{
    								self.alert1("评价失败");
    							}

							}

						}
				
					},
					error: function(data, status, xhr) {
						var json = JSON.stringify({
							"pageName": "error"
					    });
						window.location.href = "skipPage?jsonStr=" + json;

					}
				});
			});
			
			
			
			/*var method1 = function() {

					var endCourseClassParameters = {
						"courseId": courseId,
						"classEndMark": 1,
						"classId": classId,
					}

					console.log(endCourseClassParameters);
					$.ajax({
						type: "post",
						url: "endCourseClass",
						async: true,
						data: {
							"jsonStr": JSON.stringify(endCourseClassParameters)
						},

						success: function(msg) {
							var courseIds = courseId;
							var msgNew = JSON.parse(msg);

							if(msgNew.result == "success") {
								self.alert1("结课提交成功");
							} else {
								self.alert1("结课失败");
							}

						},
						error: function(data, status, xhr) {
							var json = JSON.stringify({
						"pageName": "error"
				    });
					window.location.href = "skipPage?jsonStr=" + json;

						}
					});
				}
				self.confirmEnd("你所选的课程未结束,需要提前结束吗?", method1);
				event.stopPropagation();*/
			return false;
		},
		alert1: function(content1,refresh) { //confirm提示

			Alert1.contenthead = "提示";
			Alert1.content = content1;

			Alert1.success = function() {
				if(!refresh){
					window.location.reload();
				}
				
				Alert1.hide();
			};

			Alert1.init();
			Alert1.show();
		},
		confirmEnd: function(content1, method1) {
			confirm1.contenthead = "提示";
			confirm1.content = content1;
			confirm1.success = method1;
			confirm1.init();
			confirm1.show();

		}
	};

	index.init();

});